package org.jeecg.codegeneweb;

import io.swagger.annotations.ApiModel;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.codegeneweb.enumtype.EnumModelType;
import org.jeecg.codegeneweb.mapsetfromwo.OneMapSet;
import org.jeecg.codegeneweb.mapsetfromwo.OneToManyMapSet;
import org.jeecg.codegeneweb.mapsetfromwo.TreeMapSet;
import org.jeecg.codegeneweb.mock.OneMockWebModelVo;

import org.jeecg.codegeneweb.mock.OneToManyMockWebModelVo;
import org.jeecg.codegeneweb.mock.TreeMockWebModelVo;
import org.jeecg.codegeneweb.template.ext.ExtDestinationLocation;
import org.jeecg.codegeneweb.template.ext.ExtTemplateLocaltion;
import org.jeecg.codegeneweb.util.NonceUtils;
import org.jeecg.codegeneweb.vo.WebModelVo;
import org.jeecg.common.api.vo.Result;

import org.jeecgframework.codegenerate.generate.util.SimpleFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;



/*
* 这个Controller用于接收WebVO，客户端是jpa的注解解析。OnlineController10PostFromWeb 是用于从Web页面接收原来的数据。与原有的系统进行对比
* */
@RestController
@RequestMapping({"/online"})
@Slf4j
@ApiModel(value = "OnlineGeneratorController对象", description = "文档")
@Validated
@Data
public class OnlineGeneratorController {
    @Autowired
    private FreemakerEngine freemakerEngine;
    //localhost:8080/jeecg-boot/online/codeGeneratecomplex
    @PostMapping({"/codeGeneratecomplex"})
    public Result<?> codeGeneratesingletablewithouttree(@RequestBody @Validated WebModelVo webModelVo) throws Exception {
        //List<String> stringList = null;
        //定义模板的路径   3.7.1的版本
        String templatepath="";
        ExtTemplateLocaltion templateLocaltion=new ExtTemplateLocaltion(templatepath);

        Map<String,Object> map=new HashMap<>();
        EnumModelType enumModelType=webModelVo.getModeltype();

        if(enumModelType.getStylePath().equals("default.one")) {
            //qingjia_dan 请假单
            templatepath="D:\\book20241011\\jeecg\\JeecgBoot-3.7.1\\jeecg-boot\\jeecg-module-system\\jeecg-system-biz\\src\\main\\resources\\jeecg\\code-template-online\\default\\one\\java";

            templateLocaltion.setTemplatePath(templatepath);
            //定义map的生成
            OneMapSet oneMapSet=new OneMapSet();
            oneMapSet.buildMap(map,webModelVo);


        }
        if(enumModelType.getStylePath().equals("default.tree")) {
            //商品分类 test_shoptype_tree

            templatepath="D:\\book20241011\\jeecg\\JeecgBoot-3.7.1\\jeecg-boot\\jeecg-module-system\\jeecg-system-biz\\src\\main\\resources\\jeecg\\code-template-online\\default\\tree\\java";
            templateLocaltion.setTemplatePath(templatepath);
             new TreeMapSet().buildMap(map,webModelVo);

        }
        if(enumModelType.getStylePath().equals("default.onetomany")) {
            //test_order_main
            templatepath="D:\\book20241011\\jeecg\\JeecgBoot-3.7.1\\jeecg-boot\\jeecg-module-system\\jeecg-system-biz\\src\\main\\resources\\jeecg\\code-template-online\\erp\\onetomany\\java";
            templateLocaltion.setTemplatePath(templatepath);
            OneToManyMapSet om=new OneToManyMapSet();
            om.buildMap(map,webModelVo);
            om.buildSonTableMap(map,webModelVo);
            om.buildSonTableColumnMap(map,webModelVo);

            map.put("ftl_description", webModelVo.getTableVoDomain().getTableTxt());

        }
        //补充以下map
        map.put("currentDate", NonceUtils.nowformat());
        map.put("Format",new SimpleFormat());

        //定义生成文件的路径
        ExtDestinationLocation destinationLocation=new ExtDestinationLocation();
        destinationLocation.setEntityname(webModelVo.getTableVoDomain().getEntityName());
        if(webModelVo.getTestmodulename()!=null) {
            destinationLocation.setEntitypackage(webModelVo.getTestmodulename());
        }

        //destinationLocation.setTemplateLocaltion(templateLocaltion);

        //生成文件
        freemakerEngine.processextenh(map,templateLocaltion,destinationLocation);
        return Result.ok("转换成功");

    }

    public static void main(String[] args) throws Exception {
        WebModelVo onewebModelVo= OneMockWebModelVo.initWo();
        OnlineGeneratorController onlineGeneratorController=new OnlineGeneratorController();
        onlineGeneratorController.setFreemakerEngine(new FreemakerEngineImpl());
        onlineGeneratorController.codeGeneratesingletablewithouttree(onewebModelVo);


         WebModelVo treewebModelVo= TreeMockWebModelVo.initWo();
         onlineGeneratorController.codeGeneratesingletablewithouttree(treewebModelVo);

         WebModelVo onetomanywebModelVo= OneToManyMockWebModelVo.initWo();
         onlineGeneratorController.codeGeneratesingletablewithouttree(onetomanywebModelVo);

//        Map<String, Object> map = new HashMap<>();
//        map.put("bussiPackage", "testpackage");
//        map.put("entityPackage", "testentity");
//        map.put("entityName", "testentity");
//        map.put("currentDate", "2024-10-11");
//        //对模版的文件名进行替换
//        List<String> filenames = new FreemakerEngineImpl().replace模板中的文件名及变量(files, map,new DestinationLocation());
//        DestinationLocation destinationLocation=new DestinationLocation();
//        destinationLocation.setEntityname("testentity");
//        destinationLocation.setBussi_package("businesspackage");
//        destinationLocation.setEntitypackage("testpackageentity");
//        destinationLocation.setTemplateLocaltion(templateLocaltion);
//        List<String>filenamefinal=destinationLocation.combinefilenameext(filenames);
//        for (String filename : filenamefinal) {
//            System.out.println(filename);
//        }
    }








}
